#pragma once
#include "StdAfx.h"
class GraphicsManager
{
public:
	GraphicsManager(void);
	~GraphicsManager(void);
	enum RenderFlag
	{
		Wireframe=1,
		Lighting=2,
		Textures=4,
		NormalMapping=8,
		SpecularMapping=16,
		SpecialEffects=32,
		Shadows=64,
	};
	double NearViewPlane, FarViewPlane;
	Vec3 ClearColor, DefaultClearColor;
	sptr<FrameBuffer> FB;
	sptr<GameResource> EnvMap, ShadowMap, TotalLightMap[2], DepthMap, DiffuseMap, TotalSpecularMap[2], DeferredMap, DownsampleMap[4], BlurredMap[2], BloomedMap, LightPassMap;
	sptr<GameResource> DisplayMap;
	Vec2 GetDimensions();
	CGcontext GetContext();
	void Resize(int width, int height);
	void Render();
	void InitGL(HDC hdc);
	void InitGLState();
	void Enable(RenderFlag);
	void Disable(RenderFlag);
	void ResetDimensions();
	void ClearView();
	void Draw(sptr<ModelComponent>);
	void ClearMapToColor(sptr<GameResource>, Vec3);
	void ClearDepth(sptr<GameResource>);
	void AdditiveBlend(sptr<GameResource> dest_map, sptr<GameResource> map1, sptr<GameResource> map2, Vec2 factors=Vec2(1,1));
	unsigned int GetCurLightMapID();
	unsigned int GetOtherLightMapID();
	unsigned int DebugAngle;
	int Width, Height, iMisc;
	bool bAutoShading, bEnableShaders, bDrawSkybox;
	bool bClearSpecular, bUseLightPass, bDebugDraw, bDoubleShadows;
	float BloomFactor, LightCutoff;
	float FloorWidth, FloorTiles;
private:
	int  nPixelFormat;
	unsigned int nFramesElapsed;
	HGLRC	hrc;
	HDC		hdc;
	CGcontext cgContext;
	RenderFlag Flags;
	sptr<GameResource> MainShaderResc;
	friend class SingletonManager;
	int WinWidth, WinHeight;
	unsigned int CurLightMapID;
	void Init();
	//CG init function
	void InitCg();
	//DevIL init function
	void InitIL();
	void InitMaps();
	void DrawSkybox();
	void DrawFloor();
	void DrawPawn(sptr<Actor>);
	void DrawPawnCloth(sptr<Actor>);
	void DrawModelComponent(sptr<ModelComponent>);
	void DrawScene();
	void DrawDeferred();
	void LightDeferred();
	void UpdateViewProj();
	void BuildEnvMap(Vec3 pos=Vec3(0,0,0));
	void BuildShadowMap(sptr<Actor>);
	void BuildLightMap();
	void RotateLightMap();
	void DrawFullScreenTexture(sptr<GameResource> tex_resc);
	void Downsample();
	void Blur();
	void Bloom();
	void LightPass();
	void ApplyBloom();
	void DrawTextureToTarget(sptr<GameResource> src, sptr<GameResource> dest, sptr<GameResource> shader_resc);
	void DrawHUD();
	sptr<GameResource> GetBestShader(sptr<ModelComponent>);
};

